package cn.gjculture.shopping.api.util;

import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.*;

@Component
public class FtpClient {
    @Value("${ftp.server.ip}")
    private  String FTP_ADDRESS;
    //端口号
    private static final int FTP_PORT = 21;
    @Value("${ftp.server.user}")
    private String FTP_USERNAME;
    @Value("${ftp.server.password}")
    private String FTP_PASSWORD;

    public boolean uploadFile(String originFileName, String path, InputStream input) {
        boolean success = false;
        FTPClient ftp = null;
        try {
            ftp = getClient();
            if (ftp == null) {
                return false;
            }
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
            checkDirectoryExist(ftp, path);
            ftp.changeWorkingDirectory(path);
            ftp.enterLocalPassiveMode(); // 被动模式
            ftp.storeFile(originFileName, input);
            input.close();
            ftp.logout();
            success = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ftp.isConnected()) {
                    ftp.disconnect();
                }
                if (input != null) {
                    input.close();
                }
            } catch (IOException ioe) {
            }

        }
        return success;
    }

    public boolean delFile(String filePath) {
        boolean success = false;
        FTPClient ftp = null;
        try {
            ftp = getClient();
            if (ftp == null) {
                return false;
            }
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftp.deleteFile(filePath);
            ftp.logout();
            success = true;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
        return success;
    }

    private static void checkDirectoryExist(FTPClient ftp, String path) throws IOException {
        String[] paths = path.split("/");
        for (String p : paths) {
            if (!StringUtils.isEmpty(p)) {
                ftp.makeDirectory(p);
                ftp.changeWorkingDirectory(p);
            }
        }
    }

    public  FTPClient getClient() throws IOException {
        FTPClient ftp = new FTPClient();
        ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
        ftp.setControlEncoding("UTF-8");
        ftp.connect(FTP_ADDRESS, FTP_PORT);// 连接FTP服务器
        ftp.login(FTP_USERNAME, FTP_PASSWORD);// 登录
        int reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            return ftp;
        }
        return ftp;
    }

    public static void main(String[] args) throws FileNotFoundException {
//        InputStream input=new FileInputStream(new File("C:\\Users\\admin\\Pictures\\1.jpg"));
//        uploadFile("2.jpg","/static/goods/12/32/ss/ss",input);
//        delFile("/static/goods/12/32/ss/ss","2.jpg");
    }
}
